package com.tailyou.hms.firstapp.wdg;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tailyou.hms.firstapp.R;

public class CustomVideoView extends FrameLayout {
    private VideoView vv;
    private MediaPlayer mediaPlayer;
    private ImageView ivPlayOrPause, ivSound;
    private TextView tvTimeElapsedAndTotal;
    private boolean isMute = false;
    private GestureDetector gestureDetector;
    private final Runnable prgRunnable = new Runnable() {
        @Override
        public void run() {
            updateTime();
            postDelayed(prgRunnable, 1000);
        }
    };
    public CustomVideoView(@NonNull Context context) {
        super(context);
        initView(context);
        post(prgRunnable);
    }
    public CustomVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        post(prgRunnable);
    }
    private void initView(@NonNull Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_custom_video, this, true);
        vv = findViewById(R.id.vv);
        ivPlayOrPause = findViewById(R.id.ivPlayOrPause);
        ivSound = findViewById(R.id.ivSound);
        tvTimeElapsedAndTotal = findViewById(R.id.tvTimeElapsedAndTotal);
        vv.setOnPreparedListener(mp -> {
            mediaPlayer = mp;
            mediaPlayer.start();
            updateTime();
        });
        vv.setOnCompletionListener(mp -> ivPlayOrPause.setImageResource(R.mipmap.ic_play_z));
        ivPlayOrPause.setOnClickListener(v -> doPlayOrPause());
        ivSound.setOnClickListener(v -> {
            AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            if (isMute) {
                am.setStreamMute(AudioManager.STREAM_MUSIC, false);
                ivSound.setImageResource(R.mipmap.ic_unmute);
                isMute = false;
            } else {
                am.setStreamMute(AudioManager.STREAM_MUSIC, true);
                ivSound.setImageResource(R.mipmap.ic_mute);
                isMute = true;
            }
        });
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public void onShowPress(MotionEvent e) {
                super.onShowPress(e);
                setPlaybackSpeed(mediaPlayer, 2.0f);
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                doPlayOrPause();
                return true;
            }
        });
        vv.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    setPlaybackSpeed(mediaPlayer, 1.0f);
                    break;
            }
            gestureDetector.onTouchEvent(event);
            return true;
        });
    }
    private void doPlayOrPause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            ivPlayOrPause.setImageResource(R.mipmap.ic_play_z);
        } else {
            mediaPlayer.start();
            ivPlayOrPause.setImageResource(R.mipmap.ic_pause_z);
        }
    }
    public void setVideoURI(Uri uri) {
        vv.setVideoURI(uri);
    }
    public void destroy() {
        removeCallbacks(prgRunnable);
    }
    private void setPlaybackSpeed(MediaPlayer mediaPlayer, float speed) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed));
            } else {
                mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed));
                mediaPlayer.pause();
            }
        }
    }
    private void updateTime() {
        if (mediaPlayer != null) {
            long duration = mediaPlayer.getDuration();
            long position = mediaPlayer.getCurrentPosition();
            tvTimeElapsedAndTotal.setText(String.format("%02d:%02d/%02d:%02d", position / 1000 / 60, position / 1000 % 60, duration / 1000 / 60, duration / 1000 % 60));
        }
    }
}
